package window;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.filechooser.FileFilter;

import backend.Daten;
import backend.ProbenWuerfeln;
import beans.CharBean;
import beans.ProbenBean;
import beans.VerlaufElement;

public class Window extends JFrame {

	private static final long serialVersionUID = -7971645719675276166L;

	private MyJPanel statusPanel, charsPanel, probenPanel, infoPanel,
				overviewPanel, startPanel, bestaetigungsPanel, verlaufPanel;

	private JPanel addCharPanel, addProbenPanel, editCharPanel, charsPanel2,
				charPanel, probenPanel2, probenToCharPanel, probePanel,
				verlaufPanel2, panelToAdd, buttonPanelProbe,
				buttonPanelOverview;

	private JLabel ueberschriftLabel, ausgabeLabel, labelToAdd, labelStatus,
				infoLabel, infoLabel2;

	private JButton buttonAddNewChar = new JButton(), buttonOverview,
				buttonChars, buttonProben, buttonInfo,
				buttonDelChar = new JButton(), buttonEditChar = new JButton(),
				buttonSendAddChar = new JButton(),
				buttonDelProbe = new JButton(), buttonAddProbe = new JButton(),
				buttonEditProbe = new JButton(),
				buttonProbeAbbruch = new JButton(),
				buttonCharAbbruch = new JButton(),
				buttonSendProbenEdit = new JButton(),
				buttonSendAdd = new JButton(),
				buttonOverWuerfeln = new JButton(),
				buttonUpdateProbe = new JButton(),
				buttonSendCharEdit = new JButton(),
				buttonLaden = new JButton(),
				bestaetigungsButton = new JButton(),
				buttonSchliessenSpeichern = new JButton(),
				buttonSchliessen = new JButton(),
				buttonVerlauf = new JButton();

	private JTextField textCharName, textMU, textKL, textIN, textCH, textFF,
				textGE, textKO, textKK, textProbeName, textProbenId,
				textCharId, textAt, textPa, textProbenToAdd;

	private JMenuBar menueLeiste;

	private JMenu MenueDatei, menuLookAndFeel;

	private ButtonGroup buttonGroup;

	private JMenuItem itemOpen, itemSave, itemLookAndFeel;

	private JRadioButton radioButton;

	private JComboBox probenAuswahlMenue1, probenAuswahlMenue2,
				probenAuswahlMenue3, probenAuswahl;

	private ArrayList<MyJPanel> panels = new ArrayList<MyJPanel>();

	private ArrayList<JTextField> textFields = new ArrayList<JTextField>();

	private Daten daten = new Daten();

	private ProbenWuerfeln probenW = new ProbenWuerfeln(daten);

	private Border border = BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED);

	private Window window = this;

	private File saveFile = null;

	private CharBean aktuellerChar;

	public Window() {

		this.setTitle("MeisterTool");
		try {
			UIManager
						.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		this.addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {

				JFrame frame = new JFrame();
				labelToAdd = new JLabel("Sollen die Daten gespeichert werden?");
				frame.setLayout(new FlowLayout());
				frame.add(labelToAdd, BorderLayout.NORTH);
				buttonSchliessenSpeichern = new JButton("Speichern");
				buttonSchliessenSpeichern
							.addActionListener(new buttonListender());
				buttonSchliessenSpeichern.setActionCommand("speichernEnde");
				buttonSchliessen = new JButton("schließen");
				buttonSchliessen.addActionListener(new buttonListender());
				buttonSchliessen.setActionCommand("abbruchEnde");
				frame.add(buttonSchliessenSpeichern, BorderLayout.CENTER);
				frame.add(buttonSchliessen, BorderLayout.SOUTH);
				frame.setVisible(true);
				frame.setSize(300, 300);
				frame.pack();
				frame.setLocation(window.getX(), window.getY());
			}
		});
		this.getContentPane().setLayout(new FlowLayout());
		/**
		 * Menü aufbauen
		 */
		menueLeiste = new JMenuBar();
		MenueDatei = new JMenu("Datei");
		menuLookAndFeel = new JMenu("LookAndFeel");
		itemLookAndFeel = new JMenuItem("Nimbus");
		itemLookAndFeel.addActionListener(new buttonListender());
		menuLookAndFeel.add(itemLookAndFeel);
		// itemLookAndFeel = new JMenuItem("Java");
		// itemLookAndFeel.addActionListener(new buttonListender());
		// menuLookAndFeel.add(itemLookAndFeel);
		itemLookAndFeel = new JMenuItem("System");
		itemLookAndFeel.addActionListener(new buttonListender());
		menuLookAndFeel.add(itemLookAndFeel);
		itemOpen = new JMenuItem("Laden");
		itemOpen.setActionCommand("itemopen");
		itemSave = new JMenuItem("Speichern");
		itemSave.setActionCommand("itemSave");
		buttonInfo = new JButton("    Über   ");
		buttonProben = new JButton("   Proben  ");
		buttonChars = new JButton("Charaktere");
		buttonOverview = new JButton("Überblick");
		buttonVerlauf = new JButton(" Verlauf ");
		itemOpen.addActionListener(new buttonListender());
		itemSave.addActionListener(new buttonListender());
		buttonInfo.addActionListener(new buttonListender());
		buttonChars.addActionListener(new buttonListender());
		buttonProben.addActionListener(new buttonListender());
		buttonOverview.addActionListener(new buttonListender());
		buttonVerlauf.addActionListener(new buttonListender());
		MenueDatei.add(itemOpen);
		MenueDatei.add(itemSave);
		MenueDatei.add(menuLookAndFeel);
		menueLeiste.add(MenueDatei);
		menueLeiste.add(buttonOverview);
		menueLeiste.add(buttonChars);
		menueLeiste.add(buttonProben);
		menueLeiste.add(buttonVerlauf);
		menueLeiste.add(buttonInfo);
		this.setJMenuBar(menueLeiste);
		/**
		 * PanelsBauen
		 */
		infoPanel = new MyJPanel(this);
		infoPanel.addUeberschrift(" Info ");
		charsPanel = new MyJPanel(this);
		charsPanel.addUeberschrift("  Chars ");
		statusPanel = new MyJPanel(this);
		statusPanel.addUeberschrift("  Öffen und Speichern ");
		probenPanel = new MyJPanel(this);
		probenPanel.addUeberschrift("  Proben ");
		overviewPanel = new MyJPanel(this);
		overviewPanel.addUeberschrift("  ÜberBlick ");
		verlaufPanel = new MyJPanel(this);
		verlaufPanel.addUeberschrift(" Verlauf ");
		editCharPanel = new MyJPanel(this);
		addProbenPanel = new MyJPanel(this);
		addCharPanel = new MyJPanel(this);
		probenToCharPanel = new MyJPanel(this);
		bestaetigungsPanel = new MyJPanel(this);
		ueberschriftLabel = new JLabel(" Willkommen beim MeisterTool! ");
		startPanel = new MyJPanel(this);
		startPanel.setLayout(new BorderLayout());
		startPanel.add(ueberschriftLabel, BorderLayout.NORTH);
		this.getContentPane().add(startPanel);
		panels.add(startPanel);
		startPanel.add(new JLabel(" "));
		buttonLaden = new JButton(" Laden ");
		buttonLaden.addActionListener(new buttonListender());
		startPanel.add(buttonLaden, BorderLayout.SOUTH);
		this.setPanelVisible(startPanel);
		ausgabeLabel = new JLabel(" ");
	}

	protected void addPanel(MyJPanel panel) {

		panels.add(panel);
		this.getContentPane().add(panel);
	}

	private File chooseFile(int mode) {

		JFileChooser chooser = new JFileChooser();
		if (saveFile == null) {
			saveFile = new File("save.cfg");
		}
		chooser.setSelectedFile(saveFile);
		chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		chooser.addChoosableFileFilter(new FileFilter() {

			@Override
			public boolean accept(File f) {

				if (f.isDirectory()) {
					return true;
				}
				return f.getName().toLowerCase().endsWith(".cfg");
			}

			public String getDescription() {

				return "CFGs";
			}
		});
		chooser.setMultiSelectionEnabled(false);
		if (mode == 1) {
			int erg = chooser.showOpenDialog(window);
			if (erg == JFileChooser.CANCEL_OPTION
						|| erg == JFileChooser.ERROR_OPTION) {
				return null;
			}
		} else {
			int erg = chooser.showSaveDialog(window);
			if (erg == JFileChooser.CANCEL_OPTION
						|| erg == JFileChooser.ERROR_OPTION) {
				return null;
			}
		}
		saveFile = chooser.getSelectedFile();
		return saveFile;
	}

	private void datenLaden() {

		String ausgabe = "";
		try {
			daten.allesLaden(chooseFile(1));
			ausgabe = "Die Daten wurden geladen";
		} catch (FileNotFoundException e1) {
			ausgabe = "Es muss auch was gespeichert sein^^";
		}
		if (daten.getCharList().isEmpty()) {
			CharBean charToTest = new CharBean(daten.getNewCharId(),
						"BeispielChar", 10, 11, 12, 10, 14, 12, 14, 9, 15, 17);
			daten.addChar(charToTest);
			ProbenBean probe = new ProbenBean(1, "BeispielProbe", "KK", "KO",
						"GE");
			daten.addProbe(probe);
			ausgabe = "Es wurde ein Beispielchar und eine Beispielprobe erstellt";
		}
		daten.addToVerlauf("Laden", ausgabe);
		makeStatusPanel(ausgabe);
	}

	private void makeAddProbe() {

		addProbenPanel.removeAll();
		addProbenPanel.setLayout(new GridLayout(5, 2, 5, 5));
		String attribute[] = { "MU", "KL", "IN", "CH", "FF", "GE", "KO", "KK" };
		labelToAdd = new JLabel("Name:");
		addProbenPanel.add(labelToAdd);
		textProbeName = new JTextField();
		addProbenPanel.add(textProbeName);
		labelToAdd = new JLabel("Attribut1:");
		addProbenPanel.add(labelToAdd);
		probenAuswahlMenue1 = new JComboBox(attribute);
		addProbenPanel.add(probenAuswahlMenue1);
		labelToAdd = new JLabel("Attribut2:");
		addProbenPanel.add(labelToAdd);
		probenAuswahlMenue2 = new JComboBox(attribute);
		addProbenPanel.add(probenAuswahlMenue2);
		labelToAdd = new JLabel("Attribut3:");
		addProbenPanel.add(labelToAdd);
		probenAuswahlMenue3 = new JComboBox(attribute);
		addProbenPanel.add(probenAuswahlMenue3);
		buttonProbeAbbruch = new JButton("abbrechen");
		buttonProbeAbbruch.addActionListener(new buttonListender());
		addProbenPanel.add(buttonProbeAbbruch);
		buttonSendAdd = new JButton("addprobe");
		buttonSendAdd.addActionListener(new buttonListender());
		addProbenPanel.add(buttonSendAdd);
		this.setPanelVisible(addProbenPanel);
	}

	private void makeBestaetigungsPanel(String art, String id) {

		bestaetigungsPanel.removeAll();
		String name = "";
		String comp = "";
		JButton abbruchButton = null;
		if (art.equals("Char")) {
			comp = "Char";
			name = daten.getCharById(Integer.parseInt(id)).getName();
			buttonCharAbbruch = new JButton("Abbrechen");
			abbruchButton = buttonCharAbbruch;
		}
		if (art.equals("Probe")) {
			comp = "Probe";
			name = daten.getProbeById(Integer.parseInt(id)).getName();
			buttonProbeAbbruch = new JButton("Abbrechen");
			abbruchButton = buttonProbeAbbruch;
		}
		JPanel panelToAdd = new JPanel();
		panelToAdd.setLayout(new BorderLayout());
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1, 2, 5, 5));
		labelToAdd = new JLabel("Möchtest du " + name + " wirklich löschen?");
		panelToAdd.add(labelToAdd, BorderLayout.NORTH);
		labelToAdd = new JLabel("  ");
		panelToAdd.add(labelToAdd, BorderLayout.CENTER);
		bestaetigungsButton = new JButton();
		bestaetigungsButton.setText("Ja, bitte löschen!");
		bestaetigungsButton.setActionCommand(comp + "|$|" + id);
		bestaetigungsButton.addActionListener(new buttonListender());
		abbruchButton.addActionListener(new buttonListender());
		buttonPanel.add(bestaetigungsButton);
		buttonPanel.add(abbruchButton);
		panelToAdd.add(buttonPanel, BorderLayout.SOUTH);
		bestaetigungsPanel.add(panelToAdd);
		this.setPanelVisible(bestaetigungsPanel);
	}

	private void makeChar() {

		HashMap<Integer, CharBean> chars = daten.getCharList();
		charsPanel2 = new JPanel(new GridLayout(0, 2, 5, 5));
		buttonGroup = new ButtonGroup();
		for (CharBean character : chars.values()) {
			charPanel = new JPanel(new GridLayout(4, 1, 8, 8));
			charPanel.setBorder(border);
			radioButton = new JRadioButton(character.getName());
			radioButton.setActionCommand("" + character.getCharId());
			radioButton.setSelected(true);
			buttonGroup.add(radioButton);
			charPanel.add(radioButton);
			labelToAdd = new JLabel("  MU: " + character.getMU() + "  Kl: "
						+ character.getKL() + "  IN: " + character.getIN()
						+ "  CH: " + character.getCH() + "  ");
			charPanel.add(labelToAdd);
			labelToAdd = new JLabel("  FF: " + character.getFF() + "  GE: "
						+ character.getGE() + "  KO: " + character.getKO()
						+ "  KK: " + character.getKK() + "  ");
			charPanel.add(labelToAdd);
			labelToAdd = new JLabel("  Attacke: " + character.getAttacke()
						+ "  Parade: " + character.getParade() + "  ");
			charPanel.add(labelToAdd);
			charsPanel2.add(charPanel);
		}
		if (chars.isEmpty()) {
			charsPanel2 = new JPanel(new GridLayout(0, 1, 5, 5));
			labelToAdd = new JLabel(
						" Keine Chars gefunden!  Entweder Chars laden oder neue erstellen ");
			charsPanel2.setBorder(border);
			charsPanel2.add(labelToAdd);
		}
	}

	private void makeCharPanel() {

		charsPanel.removeAll();
		this.makeChar();
		charsPanel.setLayout(new BorderLayout());
		charsPanel.add(charsPanel2, BorderLayout.NORTH);
		panelToAdd = new JPanel(new GridLayout(1, 3));
		buttonAddNewChar = new JButton("Add Charakter");
		buttonEditChar = new JButton("Edit");
		buttonEditChar.setActionCommand("editchar3");
		buttonDelChar = new JButton("Löschen");
		buttonDelChar.setActionCommand("delcharsent");
		buttonAddNewChar.addActionListener(new buttonListender());
		buttonEditChar.addActionListener(new buttonListender());
		buttonDelChar.addActionListener(new buttonListender());
		panelToAdd.add(buttonAddNewChar);
		panelToAdd.add(buttonEditChar);
		panelToAdd.add(buttonDelChar);
		charsPanel.add(panelToAdd, BorderLayout.CENTER);
		charsPanel.add(ausgabeLabel, BorderLayout.SOUTH);
		this.setPanelVisible(charsPanel);
	}

	private void makeEditChar() {

		ButtonModel model = buttonGroup.getSelection();
		String idEditChar = model.getActionCommand();
		JPanel panelToAdd = new JPanel();
		CharBean chara = daten.getCharById(Integer.parseInt(idEditChar));
		editCharPanel.removeAll();
		editCharPanel.setLayout(new BorderLayout());
		panelToAdd.setLayout(new GridLayout(6, 2, 5, 5));
		labelToAdd = new JLabel("Name:");
		panelToAdd.add(labelToAdd);
		textCharName = new JTextField(chara.getName());
		panelToAdd.add(textCharName);
		labelToAdd = new JLabel("MU:");
		panelToAdd.add(labelToAdd);
		textMU = new JTextField(chara.getMU().toString());
		panelToAdd.add(textMU);
		labelToAdd = new JLabel("KL:");
		panelToAdd.add(labelToAdd);
		textKL = new JTextField(chara.getKL().toString());
		panelToAdd.add(textKL);
		labelToAdd = new JLabel("IN:");
		panelToAdd.add(labelToAdd);
		textIN = new JTextField(chara.getIN().toString());
		panelToAdd.add(textIN);
		labelToAdd = new JLabel("CH:");
		panelToAdd.add(labelToAdd);
		textCH = new JTextField(chara.getCH().toString());
		panelToAdd.add(textCH);
		labelToAdd = new JLabel("FF:");
		panelToAdd.add(labelToAdd);
		textFF = new JTextField(chara.getFF().toString());
		panelToAdd.add(textFF);
		labelToAdd = new JLabel("GE:");
		panelToAdd.add(labelToAdd);
		textGE = new JTextField(chara.getGE().toString());
		panelToAdd.add(textGE);
		labelToAdd = new JLabel("KO:");
		panelToAdd.add(labelToAdd);
		textKO = new JTextField(chara.getKO().toString());
		panelToAdd.add(textKO);
		labelToAdd = new JLabel("KK:");
		panelToAdd.add(labelToAdd);
		textKK = new JTextField(chara.getKK().toString());
		panelToAdd.add(textKK);
		labelToAdd = new JLabel("Attacke:");
		panelToAdd.add(labelToAdd);
		textAt = new JTextField(chara.getAttacke().toString());
		panelToAdd.add(textAt);
		labelToAdd = new JLabel("Parade:");
		panelToAdd.add(labelToAdd);
		textPa = new JTextField(chara.getParade().toString());
		panelToAdd.add(textPa);
		textCharId = new JTextField("" + chara.getCharId());
		buttonCharAbbruch = new JButton("abbrechen");
		buttonCharAbbruch.setActionCommand("charAbbrechen");
		buttonCharAbbruch.addActionListener(new buttonListender());
		panelToAdd.add(buttonCharAbbruch);
		buttonSendCharEdit = new JButton("weiter");
		buttonSendCharEdit.setActionCommand("sendcharEdit");
		buttonSendCharEdit.addActionListener(new buttonListender());
		panelToAdd.add(buttonSendCharEdit);
		panelToAdd.add(ausgabeLabel);
		editCharPanel.add(panelToAdd, BorderLayout.NORTH);
		editCharPanel.add(ausgabeLabel, BorderLayout.SOUTH);
		this.setPanelVisible(editCharPanel);
	}

	private void makeEditProbe() {

		addProbenPanel.removeAll();
		addProbenPanel.setLayout(new GridLayout(5, 2, 5, 5));
		ButtonModel model = buttonGroup.getSelection();
		String idEditProbe = model.getActionCommand();
		ProbenBean probe = daten.getProbeById(Integer.parseInt(idEditProbe));
		String attribute[] = { "MU", "KL", "IN", "CH", "FF", "GE", "KO", "KK" };
		textProbenId = new JTextField(idEditProbe);
		labelToAdd = new JLabel("Name:");
		addProbenPanel.add(labelToAdd);
		textProbeName = new JTextField(probe.getName());
		addProbenPanel.add(textProbeName);
		labelToAdd = new JLabel("Attribut1:");
		addProbenPanel.add(labelToAdd);
		probenAuswahlMenue1 = new JComboBox(attribute);
		probenAuswahlMenue1.setSelectedItem(probe.getAtt1());
		addProbenPanel.add(probenAuswahlMenue1);
		labelToAdd = new JLabel("Attribut2:");
		addProbenPanel.add(labelToAdd);
		probenAuswahlMenue2 = new JComboBox(attribute);
		probenAuswahlMenue2.setSelectedItem(probe.getAtt2());
		addProbenPanel.add(probenAuswahlMenue2);
		labelToAdd = new JLabel("Attribut3:");
		addProbenPanel.add(labelToAdd);
		probenAuswahlMenue3 = new JComboBox(attribute);
		probenAuswahlMenue3.setSelectedItem(probe.getAtt3());
		addProbenPanel.add(probenAuswahlMenue3);
		buttonProbeAbbruch = new JButton("abbrechen");
		buttonProbeAbbruch.addActionListener(new buttonListender());
		addProbenPanel.add(buttonProbeAbbruch);
		buttonSendProbenEdit = new JButton("speichern");
		buttonSendProbenEdit.setActionCommand("probedit");
		buttonSendProbenEdit.addActionListener(new buttonListender());
		addProbenPanel.add(buttonSendProbenEdit);
		this.setPanelVisible(addProbenPanel);
	}

	private void makeInfoPanel() {

		infoPanel.removeAll();
		infoLabel = new JLabel(
					" MeisterTool Version 0.291 rev 59 alpha von Christian Kulenkampff ");
		infoPanel.setBorder(border);
		infoLabel2 = new JLabel(
					" Verbesserungsvorschläge und Bugs bitte an chumax@gmx.de melden ");
		infoPanel.setLayout(new GridLayout(2, 1));
		infoPanel.add(infoLabel);
		infoPanel.add(infoLabel2);
		this.setPanelVisible(infoPanel);
	}

	private void makeNewCharPanel() {

		addCharPanel.removeAll();
		addCharPanel.setLayout(new BorderLayout());
		JPanel panelToAdd = new JPanel();
		panelToAdd.setLayout(new GridLayout(6, 2, 5, 5));
		labelToAdd = new JLabel("Name:");
		panelToAdd.add(labelToAdd);
		textCharName = new JTextField();
		panelToAdd.add(textCharName);
		labelToAdd = new JLabel("MU:");
		panelToAdd.add(labelToAdd);
		textMU = new JTextField();
		panelToAdd.add(textMU);
		labelToAdd = new JLabel("KL:");
		panelToAdd.add(labelToAdd);
		textKL = new JTextField();
		panelToAdd.add(textKL);
		labelToAdd = new JLabel("IN:");
		panelToAdd.add(labelToAdd);
		textIN = new JTextField();
		panelToAdd.add(textIN);
		labelToAdd = new JLabel("CH:");
		panelToAdd.add(labelToAdd);
		textCH = new JTextField();
		panelToAdd.add(textCH);
		labelToAdd = new JLabel("FF:");
		panelToAdd.add(labelToAdd);
		textFF = new JTextField();
		panelToAdd.add(textFF);
		labelToAdd = new JLabel("GE:");
		panelToAdd.add(labelToAdd);
		textGE = new JTextField();
		panelToAdd.add(textGE);
		labelToAdd = new JLabel("KO:");
		panelToAdd.add(labelToAdd);
		textKO = new JTextField();
		panelToAdd.add(textKO);
		labelToAdd = new JLabel("KK:");
		panelToAdd.add(labelToAdd);
		textKK = new JTextField();
		panelToAdd.add(textKK);
		labelToAdd = new JLabel("Attacke:");
		panelToAdd.add(labelToAdd);
		textAt = new JTextField();
		panelToAdd.add(textAt);
		labelToAdd = new JLabel("Parade:");
		panelToAdd.add(labelToAdd);
		textPa = new JTextField();
		panelToAdd.add(textPa);
		buttonCharAbbruch = new JButton("abbrechen");
		buttonCharAbbruch.setActionCommand("abrechenchar");
		buttonCharAbbruch.addActionListener(new buttonListender());
		panelToAdd.add(buttonCharAbbruch);
		buttonSendAddChar = new JButton("addchar");
		buttonSendAddChar.addActionListener(new buttonListender());
		panelToAdd.add(buttonSendAddChar);
		addCharPanel.add(panelToAdd, BorderLayout.NORTH);
		addCharPanel.add(ausgabeLabel, BorderLayout.SOUTH);
		this.setPanelVisible(addCharPanel);
	}

	private void makeOverviewPanel() {

		overviewPanel.removeAll();
		overviewPanel.setLayout(new BorderLayout());
		this.makeChar();
		overviewPanel.add(charsPanel2, BorderLayout.NORTH);
		buttonPanelOverview = new JPanel(new GridLayout(2, 1));
		buttonOverWuerfeln = new JButton("Würfeln");
		buttonOverWuerfeln.addActionListener(new buttonListender());
		ArrayList<String> probenListe = new ArrayList<String>();
		for (ProbenBean probe : daten.getProbenList().values()) {
			probenListe.add(probe.getName());
		}
		probenAuswahl = new JComboBox(probenListe.toArray());
		buttonPanelOverview.add(probenAuswahl);
		buttonPanelOverview.add(buttonOverWuerfeln);
		overviewPanel.add(buttonPanelOverview, BorderLayout.CENTER);
		overviewPanel.add(ausgabeLabel, BorderLayout.SOUTH);
		this.setPanelVisible(overviewPanel);
	}

	private void makeProbenPanel() {

		probenPanel.removeAll();
		probenPanel.setLayout(new BorderLayout());
		probenPanel2 = new JPanel();
		probenPanel2.setLayout(new GridLayout(0, 2, 5, 5));
		buttonGroup = new ButtonGroup();
		for (ProbenBean probe : daten.getProbenList().values()) {
			probePanel = new JPanel(new GridLayout(2, 1));
			probePanel.setBorder(border);
			radioButton = new JRadioButton(probe.getName() + " ");
			radioButton.setActionCommand(probe.getIdS());
			radioButton.setSelected(true);
			// buttongroup gibt die möglichkeit den aktuellen buttonstand
			// abzufragen
			buttonGroup.add(radioButton);
			probePanel.add(radioButton);
			labelToAdd = new JLabel("  ( " + probe.getAtt1() + ", "
						+ probe.getAtt2() + ", " + probe.getAtt3() + ")   ");
			probePanel.add(labelToAdd);
			probenPanel2.add(probePanel);
		}
		if (daten.getProbenList().isEmpty()) {
			probenPanel2 = new JPanel(new GridLayout(0, 1, 5, 5));
			labelToAdd = new JLabel(
						" Keine Proben gefunden!  Entweder welche laden oder neue erstellen ");
			probenPanel2.setBorder(border);
			probenPanel2.add(labelToAdd);
		}
		probenPanel.add(probenPanel2, BorderLayout.NORTH);
		buttonPanelProbe = new JPanel(new GridLayout(1, 3));
		buttonAddProbe = new JButton("Add Probe");
		buttonEditProbe = new JButton("Edit");
		buttonEditProbe.setActionCommand("editprobe");
		buttonDelProbe = new JButton("Löschen");
		buttonDelProbe.setActionCommand("delprobe");
		buttonAddProbe.addActionListener(new buttonListender());
		buttonEditProbe.addActionListener(new buttonListender());
		buttonDelProbe.addActionListener(new buttonListender());
		buttonPanelProbe.add(buttonAddProbe);
		buttonPanelProbe.add(buttonEditProbe);
		buttonPanelProbe.add(buttonDelProbe);
		probenPanel.add(buttonPanelProbe, BorderLayout.CENTER);
		probenPanel.add(ausgabeLabel, BorderLayout.SOUTH);
		this.setPanelVisible(probenPanel);
	}

	private void makeProbenToCharPanel() {

		probenToCharPanel.removeAll();
		textFields.clear();
		probenToCharPanel.setLayout(new BorderLayout());
		JPanel panelToAdd2 = new JPanel(new GridLayout(0, 2, 5, 5));
		for (ProbenBean probe : daten.getProbenList().values()) {
			panelToAdd = new JPanel();
			panelToAdd.setLayout(new GridLayout(1, 2));
			labelToAdd = new JLabel(probe.getName() + ": ");
			textProbenToAdd = new JTextField(""
						+ aktuellerChar.getTawById(probe.getId()));
			textProbenToAdd.setToolTipText(probe.getName());
			textFields.add(textProbenToAdd);
			panelToAdd.add(labelToAdd);
			panelToAdd.add(textProbenToAdd);
			panelToAdd2.add(panelToAdd);
		}
		if (daten.getProbenList().isEmpty()) {
			panelToAdd2 = new JPanel(new GridLayout(0, 1, 5, 5));
			labelToAdd = new JLabel("Keine Proben gefunden!");
			panelToAdd2.setBorder(border);
			panelToAdd2.add(labelToAdd);
		}
		probenToCharPanel.add(panelToAdd2, BorderLayout.NORTH);
		buttonUpdateProbe = new JButton("speichern");
		buttonUpdateProbe.setActionCommand("charupdateprobe");
		buttonUpdateProbe.addActionListener(new buttonListender());
		probenToCharPanel.add(buttonUpdateProbe, BorderLayout.CENTER);
		probenToCharPanel.add(ausgabeLabel, BorderLayout.SOUTH);
		this.setPanelVisible(probenToCharPanel);
	}

	private void makeStatusPanel(String text) {

		statusPanel.removeAll();
		labelStatus = new JLabel(text);
		statusPanel.add(labelStatus);
		this.setPanelVisible(statusPanel);
	}

	private void makeVerlaufsPanel() {

		verlaufPanel.removeAll();
		verlaufPanel2 = new JPanel();
		JScrollPane scrollPane = new JScrollPane(verlaufPanel2);
		scrollPane.setPreferredSize(new Dimension(400, 410));
		verlaufPanel2.setLayout(new GridLayout(0, 1, 5, 5));
		LinkedList<VerlaufElement> verlauf = daten.getVerlauf();
		if (verlauf.isEmpty()) {
			labelToAdd = new JLabel("Der Verlauf ist Leer!");
			verlaufPanel2.add(labelToAdd);
		} else {
			for (VerlaufElement element : verlauf) {
				panelToAdd = new JPanel();
				panelToAdd.setBorder(border);
				panelToAdd.setLayout(new GridLayout(0, 1, 5, 5));
				labelToAdd = new JLabel(" " + element.getTyp() + " ");
				panelToAdd.add(labelToAdd);
				labelToAdd = new JLabel(" " + element.getTime() + " ");
				panelToAdd.add(labelToAdd);
				labelToAdd = new JLabel(" " + element.getData() + " ");
				panelToAdd.add(labelToAdd);
				verlaufPanel2.add(panelToAdd);
			}
			if (verlauf.size() < 6) {
				panelToAdd = new JPanel();
				verlaufPanel2.add(panelToAdd);
				panelToAdd = new JPanel();
				verlaufPanel2.add(panelToAdd);
				panelToAdd = new JPanel();
				verlaufPanel2.add(panelToAdd);
			}
		}
		verlaufPanel.add(scrollPane);
		this.setPanelVisible(verlaufPanel);
	}

	private void setPanelVisible(JPanel panel) {

		for (JPanel panel2 : panels) {
			panel2.setVisible(false);
		}
		panel.setVisible(true);
		this.getContentPane().repaint();
	}

	private void setLookAndFeelNimbus() {

		try {
			UIManager
						.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (InstantiationException ex) {
			ex.printStackTrace();
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
		} catch (UnsupportedLookAndFeelException ex) {
			ex.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(this);
	}

	private void setLookAndFeelSystem() {

		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		SwingUtilities.updateComponentTreeUI(this);
	}

	class buttonListender implements ActionListener {

		public void actionPerformed(final ActionEvent e) {

			if (e.getSource() instanceof JMenuItem) {
				if (e.getActionCommand().equals("Nimbus")) {
					setLookAndFeelNimbus();
				}
				if (e.getActionCommand().equals("System")) {
					setLookAndFeelSystem();
				}
			}
			if (e.getActionCommand() == buttonSchliessenSpeichern
						.getActionCommand()) {
				daten.allesSpeichern(chooseFile(2));
				System.exit(0);
			}
			if (e.getActionCommand() == buttonSchliessen.getActionCommand()) {
				System.exit(0);
			}
			if (e.getActionCommand() == buttonOverview.getText()) {
				makeOverviewPanel();
				ausgabeLabel.setText(" ");
			}
			if (e.getActionCommand() == buttonVerlauf.getText()) {
				makeVerlaufsPanel();
				ausgabeLabel.setText(" ");
			}
			if (e.getActionCommand() == buttonOverWuerfeln.getText()) {
				ButtonModel model = buttonGroup.getSelection();
				String charId = model.getActionCommand();
				Object[] list = probenAuswahl.getSelectedObjects();
				for (Object o : list) {
					String name = (String) o;
					probenW.wuerfeln(Integer.parseInt(charId), daten
								.getProbenIdByName(name));
				}
			}
			if (e.getActionCommand() == buttonOverview.getText()) {
				makeOverviewPanel();
				ausgabeLabel.setText(" ");
			}
			if (e.getActionCommand() == buttonChars.getText()) {
				makeCharPanel();
				ausgabeLabel.setText(" ");
			}
			if (e.getActionCommand() == buttonProben.getText()) {
				makeProbenPanel();
				ausgabeLabel.setText(" ");
			}
			if (e.getActionCommand() == buttonInfo.getText()) {
				makeInfoPanel();
				ausgabeLabel.setText(" ");
			}
			if (e.getActionCommand() == itemOpen.getActionCommand()) {
				datenLaden();
			}
			if (e.getActionCommand() == buttonLaden.getActionCommand()) {
				datenLaden();
			}
			if (e.getActionCommand() == itemSave.getActionCommand()) {
				daten.allesSpeichern(chooseFile(2));
				makeStatusPanel("Daten wurden gespeichert");
				daten.addToVerlauf("Speichern", " gespeichert");
			}
			if (e.getActionCommand() == buttonAddProbe.getText()) {
				makeAddProbe();
			}
			if (e.getActionCommand() == buttonEditProbe.getActionCommand()) {
				makeEditProbe();
			}
			if (e.getActionCommand() == buttonDelProbe.getActionCommand()) {
				ButtonModel model = buttonGroup.getSelection();
				String idDelProbe = model.getActionCommand();
				makeBestaetigungsPanel("Probe", idDelProbe);
			}
			if (e.getActionCommand().equals(buttonSendAdd.getText())) {
				String name = textProbeName.getText();
				ProbenBean probe = new ProbenBean(daten.getNewProbenId(), name,
							(String) probenAuswahlMenue1.getSelectedItem(),
							(String) probenAuswahlMenue2.getSelectedItem(),
							(String) probenAuswahlMenue3.getSelectedItem());
				daten.addProbe(probe);
				makeProbenPanel();
			}
			if (e.getActionCommand() == buttonSendProbenEdit.getActionCommand()) {
				String name = textProbeName.getText();
				ProbenBean probe = new ProbenBean(Integer.parseInt(textProbenId
							.getText()), name, (String) probenAuswahlMenue1
							.getSelectedItem(), (String) probenAuswahlMenue2
							.getSelectedItem(), (String) probenAuswahlMenue3
							.getSelectedItem());
				daten.updateProbe(Integer.parseInt(textProbenId.getText()),
							probe);
				makeProbenPanel();
			}
			if (e.getActionCommand() == buttonUpdateProbe.getActionCommand()) {
				boolean weiter = true;
				try {
					aktuellerChar.clearProbenList();
					for (JTextField feld : textFields) {
						aktuellerChar.addToProbenList(daten
									.getProbenIdByName(feld.getToolTipText()),
									Integer.parseInt(feld.getText()));
					}
				} catch (NumberFormatException e1) {
					ausgabeLabel
								.setText("Es müssen schon ganze Zahlen eingegeben werden.");
					weiter = false;
				}
				if (weiter) {
					makeCharPanel();
				}
				daten.addToVerlauf("UpdateProbe", "aktualisiert");
			}
			if (e.getActionCommand() == buttonAddNewChar.getText()) {
				makeNewCharPanel();
			}
			if (e.getActionCommand() == buttonDelChar.getActionCommand()) {
				ButtonModel model = buttonGroup.getSelection();
				String idDelChar = model.getActionCommand();
				makeBestaetigungsPanel("Char", idDelChar);
			}
			if (e.getActionCommand() == buttonEditChar.getActionCommand()) {
				makeEditChar();
			}
			if (e.getActionCommand() == buttonSendAddChar.getText()) {
				boolean weiter = true;
				String name = textCharName.getText();
				try {
					int mu = Integer.parseInt(textMU.getText());
					int kl = Integer.parseInt(textKL.getText());
					int in = Integer.parseInt(textIN.getText());
					int ch = Integer.parseInt(textCH.getText());
					int ff = Integer.parseInt(textFF.getText());
					int ge = Integer.parseInt(textGE.getText());
					int ko = Integer.parseInt(textKO.getText());
					int kk = Integer.parseInt(textKK.getText());
					int at = Integer.parseInt(textAt.getText());
					int pa = Integer.parseInt(textPa.getText());
					aktuellerChar = new CharBean(daten.getNewCharId(), name,
								mu, kl, in, ch, ff, ge, ko, kk, at, pa);
				} catch (NumberFormatException ex) {
					ausgabeLabel
								.setText("Es müssen für die Werte ganze Zahlen eingegeben werden!");
					weiter = false;
				}
				if (weiter) {
					makeProbenToCharPanel();
				}
				daten.addToVerlauf("AddChar", "aktualisiert");
			}
			if (e.getActionCommand() == buttonSendCharEdit.getActionCommand()) {
				aktuellerChar = daten.getCharById(Integer.parseInt(textCharId
							.getText()));
				boolean weiter = true;
				String name = textCharName.getText();
				try {
					int mu = Integer.parseInt(textMU.getText());
					int kl = Integer.parseInt(textKL.getText());
					int in = Integer.parseInt(textIN.getText());
					int ch = Integer.parseInt(textCH.getText());
					int ff = Integer.parseInt(textFF.getText());
					int ge = Integer.parseInt(textGE.getText());
					int ko = Integer.parseInt(textKO.getText());
					int kk = Integer.parseInt(textKK.getText());
					int at = Integer.parseInt(textAt.getText());
					int pa = Integer.parseInt(textPa.getText());
					int id = Integer.parseInt(textCharId.getText());
					aktuellerChar.update(id, name, mu, kl, in, ch, ff, ge, ko,
								kk, at, pa);
					daten.addToVerlauf("Charupdate", name);
				} catch (NumberFormatException ex) {
					ausgabeLabel
								.setText("Es müssen für die Werte ganze Zahlen eingegeben werden!");
					weiter = false;
				}
				if (weiter) {
					makeProbenToCharPanel();
				}
			}
			if (e.getActionCommand() == buttonCharAbbruch.getActionCommand()) {
				makeCharPanel();
			}
			if (e.getActionCommand() == buttonProbeAbbruch.getActionCommand()) {
				makeProbenPanel();
			}
			if (e.getActionCommand() == bestaetigungsButton.getActionCommand()) {
				String line = bestaetigungsButton.getActionCommand();
				String parts[] = line.split("\\|\\$\\|");
				if (parts[0].equals("Char")) {
					daten.removeChar(Integer.parseInt(parts[1]));
					daten.addToVerlauf("Char löschen", "gelöscht");
					makeCharPanel();
				} else if (parts[0].equals("Probe")) {
					daten.removeProbe(Integer.parseInt(parts[1]));
					daten.addToVerlauf("Probe löschen", "gelöscht");
					makeProbenPanel();
				} else {
					daten.addToVerlauf("Fehler", "DatenFormat korruppt");
					System.out.println("DatenFormat koruppt");
				}
			}
		}
	}
}
